﻿<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Web.Configuration" %>
<script runat="server">

    private const string Provider = "RSAProtectedConfigurationProvider";
    private const string Section = "connectionStrings";

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void BtnEncryptClick(object sender, EventArgs e)
    {
        var confg = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        var configSect = confg.GetSection(Section);

        if (configSect == null) return;
        configSect.SectionInformation.ProtectSection(Provider);
        confg.Save();
    }

    protected void BtnDecryptClick(object sender, EventArgs e)
    {
        var config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        var configSect = config.GetSection(Section);
        
        if (!configSect.SectionInformation.IsProtected) return;
        configSect.SectionInformation.UnprotectSection();
        config.Save();
    }


</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Connection Encryption</title>
</head>
<body>
    <form id="form1" runat="server">    

    <div>
    <h1>web.config Connection Encryption</h1>
    <br/>    <br/>    <br/>

    <asp:Button ID="btnEncrypt" runat="server" Text="Encrypt" onclick="BtnEncryptClick" />
    <asp:Button ID="btnDecrypt" runat="server" Text="Decrypt" onclick="BtnDecryptClick" />    
    <br/>
    
    <br/>
        NOTE: You should remove this aspx page from your server after deployment
    </div>
    </form>
</body>
</html>
